package itsm.isperp.framework.data.excel.validate;

import itsm.isperp.framework.data.excel.ImportCellDesc;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 唯一性检查
 * 
 * @author lizx
 * @date 2013-5-3
 */
public class UniqueValidator extends AbstractValidator {

	/**
	 * 唯一性包含两种
	 * <p>
	 * 第一、excel表中的唯一性检查；
	 * </p>
	 * <p>
	 * 第二、excel与数据库的唯一性检查；
	 * </p>
	 */
	public String processValidate(Map<String, Object> params) {

		List<ImportCellDesc> list = this.getRepeatCellList();

		// System.out.println(ReflectHelper.toString(this));
		if (list != null) {
			StringBuilder sb = new StringBuilder();

			int row = 0;

			int size = list.size();
			for (int i = row; i < size; i++) {
				ImportCellDesc cell = list.get(i);
				if (this.getCell().getFieldName().equals(cell.getFieldName())
						&& cell.getFieldValue().equals(this.getFieldValue())) {
					sb.append(cell.getCellRef()).append(",");
				}
			}

			if (StringUtils.isEmpty(sb.toString())) {
				return OK;
			}

			return getCellRef() + " 与 " + sb.append("数据重复！").toString();
		}

		return OK;
	}
}
